package com.battle.service.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.battle.pojo.entity.CaseBaseInfo;
import com.battle.pojo.entity.CasePatrolStation;
import com.battle.pojo.enums.CaseDispatchTypeEnum;
import com.battle.pojo.enums.CaseStatusEnum;
import com.battle.pojo.param.caseDispatch.CreateCaseDispatchParam;
import com.battle.pojo.param.casePatrolStation.*;
import com.battle.pojo.vo.casePatrolStation.*;
import com.battle.pojo.dto.casePatrolStation.*;
import com.battle.service.repository.in.CaseBaseInfoRepository;
import com.battle.service.repository.in.CasePatrolStationRepository;
import com.battle.service.service.in.CaseDispatchService;
import com.battle.service.service.in.CasePatrolStationService;
import nirvana.core.domains.vo.PageResult;
import nirvana.core.utils.ids.IdGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.ArrayList;
import java.util.List;

/**
 * 【描 述】：站巡处-战例详情服务
 * 【环 境】：J2SE 17
 * 【详 细】：
 */
@Service
public class CasePatrolStationServiceImpl implements CasePatrolStationService {

    private final CasePatrolStationRepository casePatrolStationRepository;
    private final CaseDispatchService caseDispatchService;

    public CasePatrolStationServiceImpl(CasePatrolStationRepository casePatrolStationRepository, CaseDispatchService caseDispatchService) {
        this.casePatrolStationRepository = casePatrolStationRepository;
        this.caseDispatchService = caseDispatchService;
    }


    /**
     * 创建站巡处-战例详情
     *
     * @param param
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createCasePatrolStation(CreateCasePatrolStationParam param) {
        CasePatrolStation casePatrolStation = new CasePatrolStation();
        BeanUtils.copyProperties(param, casePatrolStation);
        Long id=param.getCaseId();
        casePatrolStation.setId(id);
        casePatrolStationRepository.create(casePatrolStation);

        //指挥中心力量调派情况
        caseDispatchService.createCaseDispatch(true, param.getCommandDispatchList(), CaseDispatchTypeEnum.command, id, casePatrolStation.getSirensId());
        //出动力量
        caseDispatchService.createCaseDispatch(true, param.getDispatchList(), CaseDispatchTypeEnum.mobilizationForce, id, casePatrolStation.getSirensId());
    }

    /**
     * 修改站巡处-战例详情
     *
     * @param param
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void editCasePatrolStation(EditCasePatrolStationParam param) {
        Long id = param.getCaseId();
        CasePatrolStation casePatrolStation = casePatrolStationRepository.getByIdCheck(id);
        BeanUtils.copyProperties(param, casePatrolStation);
        casePatrolStationRepository.edit(casePatrolStation);

        //指挥中心力量调派情况
        caseDispatchService.createCaseDispatch(true, param.getCommandDispatchList(), CaseDispatchTypeEnum.command, id, casePatrolStation.getSirensId());
        //出动力量
        caseDispatchService.createCaseDispatch(true, param.getDispatchList(), CaseDispatchTypeEnum.mobilizationForce, id, casePatrolStation.getSirensId());
    }

    /**
     * 移除站巡处-战例详情
     *
     * @param id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeCasePatrolStation(Long id) {
        casePatrolStationRepository.removeById(id);
        //指挥中心力量调派情况
        caseDispatchService.createCaseDispatch(false, null, CaseDispatchTypeEnum.command, id, null);
        //出动力量
        caseDispatchService.createCaseDispatch(false, null, CaseDispatchTypeEnum.mobilizationForce, id, null);
    }

    /**
     * 启动/禁用站巡处-战例详情
     *
     * @param id
     */
    @Override
    public void enabledCasePatrolStation(Long id) {
        CasePatrolStation casePatrolStation = casePatrolStationRepository.getByIdCheck(id);
        casePatrolStation.setIsDisable(!casePatrolStation.getIsDisable());
        casePatrolStationRepository.edit(casePatrolStation);
    }

    /**
     * 获取站巡处-战例详情详情
     *
     * @param id
     * @return
     */
    @Override
    public GetCasePatrolStationVo getCasePatrolStation(Long id) {
        CasePatrolStation casePatrolStation = casePatrolStationRepository.getById(id);
        GetCasePatrolStationVo vo = new GetCasePatrolStationVo();
        if (casePatrolStation == null) {
            return vo;
        }
        BeanUtils.copyProperties(casePatrolStation, vo);

        //指挥中心力量调派情况
        vo.setCommandDispatchList(caseDispatchService.getCaseDispatchDownList(id, CaseDispatchTypeEnum.command));
        //出动力量
        vo.setDispatchList(caseDispatchService.getCaseDispatchDownList(id, CaseDispatchTypeEnum.mobilizationForce));
        return vo;
    }

    /**
     * 获取站巡处-战例详情 列表
     *
     * @param param
     * @return
     */
    @Override
    public PageResult<GetCasePatrolStationListVo> getCasePatrolStationList(GetCasePatrolStationListParam param) {

        SelectCasePatrolStationDto dto = new SelectCasePatrolStationDto();
        BeanUtils.copyProperties(param, dto);
        Page<CasePatrolStation> page = casePatrolStationRepository.selectCasePatrolStation(dto);
        List<CasePatrolStation> casePatrolStationList = page.getRecords();
        List<GetCasePatrolStationListVo> list = new ArrayList<>();
        for (CasePatrolStation casePatrolStation : casePatrolStationList) {
            GetCasePatrolStationListVo vo = new GetCasePatrolStationListVo();
            BeanUtils.copyProperties(casePatrolStation, vo);

            list.add(vo);
        }
        return new PageResult<>(list, page);

    }

    /**
     * 获取站巡处-战例详情下拉列表
     *
     * @param param
     * @return
     */
    @Override
    public List<GetCasePatrolStationDownListVo> getCasePatrolStationDownList(GetCasePatrolStationDownListParam param) {
        CasePatrolStation po = new CasePatrolStation();
        if (param != null) {
            BeanUtils.copyProperties(param, po);
        }
        List<CasePatrolStation> casePatrolStationList = casePatrolStationRepository.getListByEntity(po);

        List<GetCasePatrolStationDownListVo> list = new ArrayList<>();
        for (CasePatrolStation casePatrolStation : casePatrolStationList) {
            GetCasePatrolStationDownListVo vo = new GetCasePatrolStationDownListVo();
            BeanUtils.copyProperties(casePatrolStation, vo);
            list.add(vo);
        }
        return list;
    }


}



